package cn.xw;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xiaoYang
 * @Date: 2021/4/13 14:29
 * @Description:
 */
public class GeneratorCodeUtils {
    public static void main(String[] args) {
        //别忘了在mapper加@Mapper
        //别忘了写完加@MapperScan注解扫描
        //设置作者信息
        String author = "xiaoYang";
        //数据库URL / username / password
        String url = "jdbc:mysql://localhost:3306/demo_mp?useSSL=true&useUnicode=true&" +
                "characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&" +
                "useLegacyDatetimeCode=false&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123";
        //这个include 和exclude只能存在一个
        String[] include = {"student"};
        String[] exclude = {};
        //乐观锁属性名称 和数据库表字段保持一致
        String version = "version";
        //逻辑删除属性名称 和数据库表字段保持一致
        String isDel = "is_del";

        //代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        //全局相关配置
        GlobalConfig gc = new GlobalConfig();
        String propertyPath = System.getProperty("user.dir");
        gc.setOutputDir(propertyPath + "./mybatis_plus_generator/src/main/java")
                .setAuthor(author)
                .setDateType(DateType.ONLY_DATE)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setOpen(false)
                .setServiceName("%sService");
        autoGenerator.setGlobalConfig(gc);
        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl(url);
        dsc.setUsername(username);
        dsc.setPassword(password);
        autoGenerator.setDataSource(dsc);
        //包配置
        PackageConfig pgc = new PackageConfig();
        pgc.setParent("cn.xw");
        autoGenerator.setPackageInfo(pgc);
        //自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        String templatePath = "/templates/mapper.xml.ftl";
        ArrayList<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return propertyPath + "./mybatis_plus_generator/src/main/resources/mapper/" + pgc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);
        //模板信息配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);

        //其它信息配置
        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(include);
        //排除生成的表名称，就是写数据库表名称 String ... include
        strategy.setExclude(exclude);
        //strategy.setFieldPrefix("");
        strategy.setTablePrefix("");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //表填充字段
        List<TableFill> fill = new ArrayList<>();
        fill.add(new TableFill("create_time", FieldFill.INSERT));
        fill.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(fill);
        strategy.setVersionFieldName(version);
        strategy.setLogicDeleteFieldName(isDel);
        autoGenerator.setStrategy(strategy);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }
}